package com.ymatou.productstock.api.testcase;

import java.util.Date;
import java.util.Map;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.productstock.api.service.UpdateProductActivityStockTimeCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AssertService;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.database.model.YmtProductActivityStock;
import com.ymttest.database.mongo.ProductActivityStockInfoWapper;
import com.ymttest.database.mongo.YmtMongoDriverFactroy;
import com.ymttest.database.sqlwapper.YmtProductActivityStockWapper;
import com.ymttest.database.sqlwapper.YmtProductsInActivityWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.YMTDateUtil;

public class Ts_UpdateProductActivityStockTime {
	//base data
	private static String sellerName = EnvSetup.getData("productsellername");
	private static int sellerId = Integer.parseInt(EnvSetup.getData("productsellerid"));
	private static int activityStartId = Integer.parseInt(EnvSetup.getData("productActivityStartId"));
	private static String activityUser = EnvSetup.getData("AdminLoginUser");
	private static String activityPswd = EnvSetup.getData("AdminLoginPswd");
	
	//sql db
	private static YmtProductActivityStockWapper ymtProductActivityStockWapper = new YmtProductActivityStockWapper();
	private static YmtProductsInActivityWapper ymtProductsInActivityWapper = new YmtProductsInActivityWapper();

	//mongo db
	private static ProductActivityStockInfoWapper productActivityStockInfoWapper= new ProductActivityStockInfoWapper();
	
	//API
	private static UpdateProductActivityStockTimeCall updateProductActivityStockTimeCall = new UpdateProductActivityStockTimeCall();
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("修改促销时间");
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(updateProductActivityStockTimeCall.getOpurl());
	}
	
	@After
	public void caseDown() {
		Logger.end();
	}

	
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_UpdateProductActivityStockTime_001() {
		Logger.start(true, "修改促销时间");
		try{
			String productId = SellerProductCallService.addNewProductAndApproveActivity(sellerId,
					sellerName,activityUser,activityPswd,String.valueOf(activityStartId),null);
			
			
			String updateStartTime = YMTDateUtil.getBeforeOrNextDay(1,YMTDateUtil.YMDTHMS_STRING);
			String updateEndTime = YMTDateUtil.getBeforeOrNextDay(2,YMTDateUtil.YMDTHMS_STRING);
			
			int productinActivityId = ymtProductsInActivityWapper.selectByProductId(productId).getIproductinactivityid();
			
			Map<String, Object> updateMap = MapUtil.hashMap("UserID", sellerId, "ActivityID", activityStartId, "ProductId", productId,
															"ProductActivityId",productinActivityId,"BeginTime",updateStartTime,"EndTime",updateEndTime);
			
			JSONObject retBean = updateProductActivityStockTimeCall.callAndGetReturnData(updateMap);
			Logger.verifyEquals(200, retBean.getInt("Code"), "响应报文-返回码");
			
			
			//check db
			
			//SQL-Ymt_ProductActivityStock 活动时间
			YmtProductActivityStock ymtProductActivityStock=ymtProductActivityStockWapper.selectByProductId(productId).get(0);
			AssertService.assertTime(YMTDateUtil.parseDate(updateStartTime, YMTDateUtil.YMDTHMS_STRING), ymtProductActivityStock.getDbegintime(),10);
			AssertService.assertTime(YMTDateUtil.parseDate(updateEndTime, YMTDateUtil.YMDTHMS_STRING), ymtProductActivityStock.getDendtime(),10);
			

			//check mongo
			if (YmtMongoDriverFactroy.isOldMongoCheck()){
				//Mongo-ProductActivityStockInfo
				Map<String, Object> productActivityStockInfoMap = productActivityStockInfoWapper.getByProductId(productId);
				AssertService.assertTime(YMTDateUtil.parseDate(updateStartTime, YMTDateUtil.YMDTHMS_STRING), 
													(Date)productActivityStockInfoMap.get("BeginTime"),10);
				
				AssertService.assertTime(YMTDateUtil.parseDate(updateEndTime, YMTDateUtil.YMDTHMS_STRING), 
													(Date)productActivityStockInfoMap.get("EndTime"),10);
				
			}
			
			if (YmtMongoDriverFactroy.isNewMongoCheck()){
				//todo
			}
			
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
}
